package logic 
{
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import gc.states;
	
	import logic.CustomEvent;
	import gc.events;
	
	public class EventDispatch extends MovieClip
	{
		//logic
		private var stageRef:DisplayObjectContainer;
		private var statemachine:StateMachine;
		
		//events
		private var mouseDown:Boolean;
		private var keyDown:Boolean;
		
		private var keyboardBusy:Boolean;
		private var mouseBusy:Boolean;
		
		public function isKeyboardBusy():Boolean
		{
			return keyboardBusy;
		}
		
		public function setKeyboardBusy(b:Boolean):void
		{
			keyboardBusy = b;
		}
		
		public function setMouseBusy(b:Boolean):void
		{
			mouseBusy = b;
		}
		
		public function isMouseBusy():Boolean
		{
			return mouseBusy;
		}
		
		public function setKeyDown(b:Boolean):void
		{
			keyDown = b;
		}
		
		public function isKeyDown():Boolean
		{
			return keyDown;
		}
		
		public function setMouseDown(b:Boolean):void
		{
			mouseDown = b;
		} 
		
		public function isMouseDown():Boolean
		{
			return mouseDown;
		}
		
		
		private function mousedown_function(e:MouseEvent):void
		{
			switch(statemachine.getState())
			{
				case states.NOW_BUILDING:
				dispatchEvent(new CustomEvent(events.BUILDING_PLACED, [mouseX, mouseY]));
			}
		}
		
		private function mousedown_handler(e:MouseEvent):void
		{
			setMouseDown(true);
			
			if (!isMouseBusy())
			{
				mousedown_function(e);
			}
		}
		
		private function mouseup_handler(e:MouseEvent):void
		{
			setMouseDown(false);
		}
		
		private function keydown_function(key:uint):void
		{
			
			switch(key)
			{
				case 49:
				dispatchEvent( new CustomEvent(events.START_BUILD, 0));
				break;	
				
				case 50:
				dispatchEvent(new CustomEvent(events.START_BUILD, 1));
				break;
			}
		}
		
		private function keydown_handler(e:KeyboardEvent):void
		{
			setKeyDown(true);
			
			//check statemachine
			
			if (!isKeyboardBusy() && statemachine.getState() == states.FREE)
			{
				keydown_function(e.keyCode);
			}
		}
				
		
		private function keyup_handler(e:KeyboardEvent):void
		{	
			setKeyDown(false);
				
		}
	
		public function init(e:Event):void
		{
			stageRef = parent;
			//set flags
			{
				setKeyboardBusy(false);
				setMouseBusy(false);
				
				setMouseDown(false);
				setKeyDown(false);
			}
			
			//add event listeners
			{
				stageRef.addEventListener(MouseEvent.MOUSE_DOWN, mousedown_handler);
				stageRef.addEventListener(MouseEvent.MOUSE_UP, mouseup_handler);
				stageRef.addEventListener(KeyboardEvent.KEY_DOWN, keydown_handler);
				stageRef.addEventListener(KeyboardEvent.KEY_UP, keyup_handler);
			}
		}
		public function EventDispatch(s:StateMachine)
		{
			statemachine = s;
			addEventListener(Event.ADDED_TO_STAGE, init);
			
		}
		
		
	}

}